# [VGG-flowers]
# https://www.robots.ox.ac.uk/~vgg/data/flowers/17/index.html
# function: 对花卉分组

import os
import shutil
import sys

# 生成目标文件夹
aim_path = r'/home/wssysh/data/datasets/flowers/'

# 创建目标文件夹
if not os.path.exists(aim_path):
    os.mkdir(aim_path)

# loop循环变量
loop = 0

# 对源文件夹内的文件进行遍历
source_path = "/home/wssysh/Downloads/jpg"

file_list = os.listdir(source_path)
file_list.sort()
# 去除后缀的排序方法，待研究
# file_list.sort(key=lambda x:int(x[:-4]))
for imgfile in file_list:
    if loop % 80 == 0:
        son_path = os.path.join(aim_path, str(int(loop / 80)))
        if not os.path.exists(son_path):
            os.mkdir(son_path)
        # print("---分类---")
    loop += 1

    # 调用系统命令 复制 failed
    # linux与win有所不同
    # os.system('cp i son_path')
    img_path = os.path.join(source_path, imgfile)
    # 使用shutil库复制文件
    try:
        shutil.copy(img_path, son_path)
    except IOError as e:
        print("Unable to copy file. %s" % e)
        sys.exit(1)
    except:
        print("Unexpected error:", sys.exc_info())
        sys.exit(1)

print("Everything is OK!")
os.system("dolphin %s" %aim_path)